import java.io.PrintWriter;
import java.util.Arrays;

public class MyArrayList {
    public int[] elem; //定义一个操作数组
    public int usedSize; //定义一个 "已使用空间"
    public MyArrayList() {
        this.elem = new int[10];
    }
    @Override
    public String toString() {
        String str = "";
        for (int i = 0; i < size(); i++) {
            if (i == 0){
                str += "[";
                str += elem[i];
                str += ",";
                continue;
            }
            if (i == size()-1) {
                str += elem[i];
                str += "]";
                continue;
            }
            str += elem[i];
            str += ",";
        }
        return str;
    }
    public void add(int data) { //添加元素到末尾.
        if (this.isFull()) { //如果数组满了, 对数组进行扩容.
            elem = Arrays.copyOf(elem, 2*elem.length);
        }
        elem[usedSize] = data;
        usedSize ++;
    }

    public void add(int index, int data) { // 添加元素到指定位置.
        if (this.isFull()) {
            elem = Arrays.copyOf(elem, 2*elem.length);
        }
        for (int i = size()-1; i >= index; i--) {
            elem[i+1] = elem[i]; // 把元素向后移一位.(把i位置的元素赋到它后面的位置)
        }
        elem[index] = data;
        usedSize++;
    }
    private boolean isFull () {
        if (usedSize == elem.length) {
            return true;
        }
        else {
            return false;
        }
    }

    public void remove (int data) {  //根据值删除元素
        for (int i = 0; i < size(); i++) {
            if (elem[i] == data) { //先找到指定元素的下标
                for (int j = i; j < size()-1; j++) {
                    elem[j]= elem[j+1]; // 往前覆盖掉elem[i].
                }
                usedSize--;
            }
        }
    }
    public void  removeIndex (int index) { // 根据下标删除元素
        for (int j = index; j < size()-1; j++) {
            elem[j] = elem[j+1];
        }
        usedSize--;
    }
    public int size() { // 求当前顺序表的长度.
        return this.usedSize;
    }
    public boolean contains(int data) { // 判断是否包含指定元素data
        for (int i = 0; i < size(); i++) {
            if (elem[i] == data) {
                return true;
            }
        }
        return false;
    }
    public void clear() { // 清空当前顺序列表.
        this.usedSize = 0;
    }
    public boolean isEmpty() {  //判断当前顺序列表是否为空
        if (this.usedSize == 0) {
            return true;
        }
        else {
            return false;
        }
    }
}
